package com.mlearning.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.mlearning.demo.pojo.neo4j.Book;
import com.mlearning.demo.pojo.neo4j.User;
import com.mlearning.demo.service.BookService;
import com.mlearning.demo.service.TreeService;
import com.mlearning.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Integer.parseInt;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    @RequestMapping("/recommend")
    public String recommend(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        model.addAttribute("recBooks",bookService.getRecBooks(user.getId()));
        return "user/recommend";
    }
    @RequestMapping(value = "/book")
    public String books(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        String search;
        if (request.getParameter("search") != null) {
            search = request.getParameter("search");
            ArrayList<Book> books=bookService.getRelBooks(search);
            int page_num=books.size()/7;
            if(books.size()%7>0){
                page_num+=1;
            }

            int next_page;
            if (request.getParameter("page") == null){
                next_page = 1;
            }else {
                next_page = parseInt(request.getParameter("page"));
            }

            model.addAttribute("now_page",next_page);
            model.addAttribute("page_num",page_num);
            model.addAttribute("books",books);
            model.addAttribute("search",search);
            if(user!=null){
                model.addAttribute("trees",userService.getMyTrees(user.getId()));
                model.addAttribute("isMastered",userService.isMastered(user.getId(),search));
            }
        }

        return "base/book";
    }
    @RequestMapping("/book_detail")
    public String book_detail(HttpServletRequest request,Model model) {
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        if (request.getParameter("id") != null){
            int id = parseInt(request.getParameter("id"));
            Book book = bookService.getBookDetail(id);
            if(user!=null){
               model.addAttribute("isSave",bookService.isSave(user.getId(),book.getId()));
            }
            ArrayList<String> tags = bookService.getBookTags(id);
            ArrayList<String> knows = bookService.getBookKnows(id);
            JSONObject bookInfo=JSONObject.parseObject(book.getBookInfo());

            model.addAttribute("book",book);
            model.addAttribute("bookInfo",bookInfo);
            model.addAttribute("tags",tags);
            model.addAttribute("knows",knows);
            return "base/book_detail";
        }else {
            return "/";
        }
    }
}
